//
//  ViewController.m
//  test
//
//  Created by fcn on 2020/8/10.
//  Copyright © 2020 hdj. All rights reserved.
//

#import "ViewController.h"

@interface ViewController()<NSCollectionViewDataSource>

@property (weak) IBOutlet NSCollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray<NSMutableArray<MYCExcelCell*>*> *dataArr;

@property (nonatomic, strong) NSMutableArray<AccountModel*> *accountModelArr;
@property (nonatomic, strong) NSMutableArray<GoodsModel*> *goodsModelArr;
@property (nonatomic, strong) NSMutableArray<DetailModel*> *detailModelArr;


@end

@implementation ViewController

#pragma mark - lazy load
- (NSMutableArray<NSMutableArray<MYCExcelCell*>*> *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray new];
    }
    return _dataArr;
}
- (NSMutableArray<AccountModel *> *)accountModelArr{
    if (!_accountModelArr) {
        _accountModelArr = [NSMutableArray array];
    }
    return _accountModelArr;
}

- (NSMutableArray<GoodsModel *> *)goodsModelArr{
    if (!_goodsModelArr) {
        _goodsModelArr = [NSMutableArray array];
    }
    return _goodsModelArr;
}

- (NSMutableArray<DetailModel *> *)detailModelArr{
    if (!_detailModelArr) {
        _detailModelArr = [NSMutableArray array];
    }
    return _detailModelArr;
}


#pragma mark - vc func
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

#pragma mark - sel
- (IBAction)readGoodsList:(id)sender {
    NSArray* goodModelArr = [GoodsModel readGoodsList];
    [self.goodsModelArr removeAllObjects];
    [self.goodsModelArr addObjectsFromArray:goodModelArr];
}

- (IBAction)readOrderList:(id)sender {
    NSArray* accountModelArr = [AccountModel readOrderList];
    [self.accountModelArr removeAllObjects];
    [self.accountModelArr addObjectsFromArray:accountModelArr];
}

- (IBAction)readDetailList:(id)sender {
    NSArray* detailModelArr = [DetailModel readDetailList];
    [self.detailModelArr removeAllObjects];
    [self.detailModelArr addObjectsFromArray:detailModelArr];
}

- (IBAction)calculate:(id)sender {
    NSArray* goodModelArr = [GoodsModel readGoodsList];
    NSArray* accountModelArr = [AccountModel readOrderList];
    NSArray* detailModelArr = [DetailModel readDetailList];
    
    NSMutableDictionary* order2price_dic = [NSMutableDictionary new];
    // 为detailModelArr的订单商品获取采购价
    for (DetailModel* detailModel in detailModelArr) {
        for (GoodsModel* goodModel in goodModelArr) {
            if ([detailModel.title containsString:goodModel.keyWord]) {
                if ([detailModel.title containsString:@"礼品礼盒"]) {
                    if ([detailModel.sku containsString:goodModel.goodName]) {
                        detailModel.purchaseCost = goodModel.purchasePrice;
                    }
                }else{
                    detailModel.purchaseCost = goodModel.purchasePrice;
                }
            }
        }
        NSNumber* num = [order2price_dic objectForKey:detailModel.orderNO];
        if (num) {
            order2price_dic[detailModel.orderNO] = @([num doubleValue] + detailModel.purchaseCost);
        }else{
            order2price_dic[detailModel.orderNO] = @(detailModel.purchaseCost);
        }
    }
    
        
    double sum = 0.0;
    // 为accountModelArr的订单计算应付总采购价
    for (AccountModel* accountModel in accountModelArr) {
        accountModel.purchaseCost = [order2price_dic[accountModel.orderNO] doubleValue];
        if (!accountModel.isAgent) {
            sum += accountModel.purchaseCost;
        }
    }
    NSLog(@"应付采购货款为： %.2lf", sum);
}



#pragma mark - NSCollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView{
    return self.dataArr.count;
}

- (NSInteger)collectionView:(nonnull NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr[section].count;
}

- (nonnull NSCollectionViewItem *)collectionView:(nonnull NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYCExcelCell* cell = self.dataArr[indexPath.section][indexPath.item];

    CollectionViewItem *item = [collectionView makeItemWithIdentifier:@"CollectionViewItem" forIndexPath:indexPath];
    item.textLab.stringValue = [NSString stringWithFormat:@"%@", cell.stringValue];
    return item;
}

#pragma mark - method
- (void)readFile {

    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setCanChooseFiles:YES];//是否能选择文件file
    [panel setCanChooseDirectories:YES];//是否能打开文件夹
    [panel setAllowsMultipleSelection:NO];//是否允许多选file
    NSInteger finded = [panel runModal]; //获取panel的响应
    
    if (finded == NSModalResponseOK) {
        // 文件url
        NSURL* url = [panel URLs].firstObject;
        NSLog(@"%@", url);
    }
}


@end
